# -*- coding: utf-8 -*-
"""
数据模型定义
使用SQLAlchemy ORM
"""

from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, Float, JSON
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class AlertRecord(Base):
    """告警记录表"""
    __tablename__ = 'alert_records'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    alert_type = Column(String(50), nullable=False, index=True)
    alert_level = Column(String(20), nullable=False, index=True)
    database_name = Column(String(255), index=True)
    table_name = Column(String(255), index=True)
    operation_user = Column(String(100))
    operation_time = Column(DateTime, index=True)
    details = Column(JSON)
    alert_sent = Column(Boolean, default=False)
    alert_time = Column(DateTime, default=datetime.now, index=True)
    alert_message = Column(Text)
    resolved = Column(Boolean, default=False)
    resolved_time = Column(DateTime)
    resolved_by = Column(String(100))
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'alert_type': self.alert_type,
            'alert_level': self.alert_level,
            'database_name': self.database_name,
            'table_name': self.table_name,
            'operation_user': self.operation_user,
            'operation_time': self.operation_time.isoformat() if self.operation_time else None,
            'details': self.details,
            'alert_sent': self.alert_sent,
            'alert_time': self.alert_time.isoformat() if self.alert_time else None,
            'resolved': self.resolved,
            'resolved_time': self.resolved_time.isoformat() if self.resolved_time else None,
            'resolved_by': self.resolved_by
        }


class MetadataSnapshot(Base):
    """元数据快照表"""
    __tablename__ = 'metadata_snapshots'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    snapshot_time = Column(DateTime, default=datetime.now, index=True)
    database_name = Column(String(255), index=True)
    table_name = Column(String(255), index=True)
    partition_count = Column(Integer, default=0)
    row_count = Column(Integer, default=0)
    total_size = Column(Integer, default=0)
    num_files = Column(Integer, default=0)
    table_type = Column(String(50))
    owner = Column(String(100))
    create_time = Column(DateTime)
    last_access_time = Column(DateTime)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'snapshot_time': self.snapshot_time.isoformat() if self.snapshot_time else None,
            'database_name': self.database_name,
            'table_name': self.table_name,
            'partition_count': self.partition_count,
            'row_count': self.row_count,
            'total_size': self.total_size,
            'num_files': self.num_files,
            'table_type': self.table_type,
            'owner': self.owner,
            'create_time': self.create_time.isoformat() if self.create_time else None,
            'last_access_time': self.last_access_time.isoformat() if self.last_access_time else None
        }


class MonitorMetrics(Base):
    """监控指标表"""
    __tablename__ = 'monitor_metrics'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    metric_time = Column(DateTime, default=datetime.now, index=True)
    metric_name = Column(String(100), index=True)
    metric_value = Column(Float)
    metric_unit = Column(String(20))
    tags = Column(JSON)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'metric_time': self.metric_time.isoformat() if self.metric_time else None,
            'metric_name': self.metric_name,
            'metric_value': self.metric_value,
            'metric_unit': self.metric_unit,
            'tags': self.tags
        }


class RuleConfig(Base):
    """规则配置表"""
    __tablename__ = 'rule_configs'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    rule_name = Column(String(100), unique=True, nullable=False)
    rule_type = Column(String(50), nullable=False)
    enabled = Column(Boolean, default=True)
    priority = Column(Integer, default=0)
    conditions = Column(JSON)
    actions = Column(JSON)
    description = Column(Text)
    created_time = Column(DateTime, default=datetime.now)
    updated_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    created_by = Column(String(100))
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'rule_name': self.rule_name,
            'rule_type': self.rule_type,
            'enabled': self.enabled,
            'priority': self.priority,
            'conditions': self.conditions,
            'actions': self.actions,
            'description': self.description,
            'created_time': self.created_time.isoformat() if self.created_time else None,
            'updated_time': self.updated_time.isoformat() if self.updated_time else None,
            'created_by': self.created_by
        }


class SystemStatus(Base):
    """系统状态表"""
    __tablename__ = 'system_status'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    status_time = Column(DateTime, default=datetime.now, index=True)
    component = Column(String(50), nullable=False)
    status = Column(String(20), nullable=False)  # running, stopped, error
    message = Column(Text)
    metrics = Column(JSON)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'status_time': self.status_time.isoformat() if self.status_time else None,
            'component': self.component,
            'status': self.status,
            'message': self.message,
            'metrics': self.metrics
        }

